import { baseUrl } from '@/config/config'
import { requestCode } from '@/config/requestCode'

// 后端返回类型 可修改
interface requestType {
  code: number
  message: string
  result: any
}

export default ({ method, url, data = {} }: any): Promise<any> => {
  return new Promise((resolve, reject) => {
    const header = {
      'content-type': 'application/json',
    }
    uni.request({
      url: baseUrl + url,
      data,
      method,
      header,
      withCredentials: true,
      success: ({ data, header }) => {
        const res = data as requestType
        // 状态码
        const status = res.code == 0 || res.code == 200 ? 200 : res.code
        const message = res.message || '系统繁忙'
        if (status === requestCode.SUCCESS) {
          return resolve(res.result)
        } else if (status !== requestCode.SUCCESS) {
          uni.showToast({
            icon: 'none',
            title: message,
          })
        }
        return reject(res)
      },
      fail(err) {
        reject(err)
      },
    })
  })
}
